package com.hudongwx.yuan_sdjj.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hudongwx.yuan_sdjj.R;
import com.hudongwx.yuan_sdjj.utils.API.API;
import com.hudongwx.yuan_sdjj.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_label_browser)
public class LabelBrowserActivity extends AppCompatActivity {
    //初始化控件
    @ViewInject(R.id.label_browser_lv)
    private ListView listView;
    @ViewInject(R.id.label_browser_tv)
    private TextView tv ;
    private MyAdapter myAdapter;
    private String TAG="LabelBrowserActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        //获得网页链接和网页名字
        String url = getIntent().getStringExtra("link");
        String [] data = url.split("\\|");
        String link = data[0];
        tv.setText(data[1]);
        Log.i(TAG, "onCreate: "+link);
        //把链接丢进去
        loadingUrl(link);


    }
    public void loadingUrl(String link){
        RequestParams requestParams = new RequestParams(API.getUrlLabel(link));
        x.http().get(requestParams, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                myAdapter = new MyAdapter(result);
                listView.setAdapter(myAdapter);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    class MyAdapter extends BaseAdapter{
        JSONObject allData;
        public MyAdapter(JSONObject allData) {
            this.allData = allData;
        }
        //第一个item的资源
        public String  getFirstItem(JSONObject data){
            String url="";
            try {
               url = data.getJSONObject("brand").getString("brandUrl");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.i(TAG, "getFirstItem: "+url);
            return url;
        }
        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(0==position){
                //加载第一个item
                WebView webView = new WebView(LabelBrowserActivity.this);
                webView.getSettings().setJavaScriptEnabled(true);
                if (convertView!=null && convertView instanceof WebView){
                    return convertView;
                }else {
                    webView.loadUrl(getFirstItem(allData));
                    return webView;
                }
            }else {
                //把下面的列表加载进来
                ImageView imageView =null;
                if(convertView!=null && convertView instanceof ImageView){
                    //附用
                    imageView = (ImageView) convertView;
                }else {
                    View view = getLayoutInflater().inflate(R.layout.belong_list_view, null);
                    imageView = (ImageView) view.findViewById(R.id.belong_list_iv);
                }
                try {
                    JSONObject item = allData.getJSONArray("data").getJSONObject(position - 1);
                    String imgUrl = item.getString("imgUrl");
                    x.image().bind(imageView,imgUrl);
                    return imageView;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return new TextView(LabelBrowserActivity.this);
        }
    }
    //返回键拦截
    @Event(R.id.label_browser_iv_back)
    private void back(View view){
        Log.i(TAG, "goBack: ");
        super.onBackPressed();
    }

}
